package com.ad.pet.app.account.record;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.account.adapter.PointListAdapter;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.models.remote.account.Point;
import com.ad.pet.models.remote.account.YueDetail;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by Administrator on 2020/2/20.
 */

public class PointDetailActivity extends AppToolbarActivity implements PointDetailContracts.View{
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private int page = 1;

    @BindView(R.id.recycler_point)
    RecyclerView mRecyclerView;
    @Inject
    PointDetailPresenter pointDetailPresenter;

    private PointListAdapter mOrderTypeAdapter;
    private TextView tvCount;
    private View view2;
    private View view1;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        mToolbar.setTitle("我的积分");

        mToolbar.getTitleView().setTextColor(getResources().getColor(R.color.white));
        mToolbar.setLeftIcon(R.drawable.whitefanhui);
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_point_detail;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mStatusLayoutManager.showContent();

        pointDetailPresenter.onCreate(this);


        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mOrderTypeAdapter = new PointListAdapter(null);
        mRecyclerView.addItemDecoration(new SimpleDividerDecoration(this, 6, R.color.bg_color));
        mRecyclerView.setAdapter(mOrderTypeAdapter);
        mOrderTypeAdapter.bindToRecyclerView(mRecyclerView);


        view1 = getLayoutInflater().inflate(R.layout.item_bing_edu,(ViewGroup) getWindow().getDecorView(),false);
        TextView tvSys= view1.findViewById(R.id.tv_sys);
        tvSys.setVisibility(View.GONE);
        tvCount = view1.findViewById(R.id.tv_count);

        view2 = getLayoutInflater().inflate(R.layout.item_tv_tile,(ViewGroup) getWindow().getDecorView(),false);

        mOrderTypeAdapter.addHeaderView(view1);
        mOrderTypeAdapter.addHeaderView(view2);
    }

    @Override
    protected void initData() {

        pointDetailPresenter.getPointDetail(String.valueOf(page));

    }

    @Override
    protected void initEvents() {

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mOrderTypeAdapter.setEnableLoadMore(false);
                mSwipeRefreshLayout.setRefreshing(false);
                pointDetailPresenter.getPointDetail(String.valueOf(page));

            }
        });

        mOrderTypeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                pointDetailPresenter.getPointDetail( String.valueOf(++page));
            }
        }, mRecyclerView);

    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent())

                .activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getPointDetailSuccess(Point detail) {
        mStatusLayoutManager.showContent();
        mSwipeRefreshLayout.setRefreshing(false);

        if (page == 1) {
            mOrderTypeAdapter.setNewData(detail.getPoint_records_list());
            if (detail.getPoint_records_list().size()==0){
                view2.setVisibility(View.GONE);
            }
        } else {
            mOrderTypeAdapter.addData(detail.getPoint_records_list());
            mOrderTypeAdapter.loadMoreComplete();
        }
        if (detail.getPoint_records_list().size() < 10) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            mOrderTypeAdapter.loadMoreEnd();
        }

        tvCount.setText(detail.getPoint());

    }

    @Override
    public void getYueDetailSuccess(YueDetail yueDetail) {

    }
}
